<?php

/**
 * game actions.
 *
 * @package    dylemat
 * @subpackage game
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class rulesActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        
    }
    
    public function executeDrawMatch(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        
        $oMatch=  GameMatchPeer::retrieveByPK($request->getParameter('id'));
        $oMatch->setResult($request->getParameter('type'));
        $oMatch->setStatus(GameMatch::$GAMEMATCH_STATUS_ENDED);
        $oMatch->save();
        return $this->renderText($request->getParameter('type'));
        //$this->redirect('@show_tournament?id='.$oMatch->getTournamentId());        
        
    }
    
    public function executeRules(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        $this->page = ($request->getParameter('page') == null) ? 1 : $request->getParameter('page');
    }

    public function executeAddRule(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        $c = new Criteria();
        //$c->add(rulesPeer::ID, 13);
        //$this->pager = new PropelPager($c, 'rulesPeer', 'doSelect', $this->Page, 12);
    }

    public function executeShowRule(sfWebRequest $request) {
        $c = new Criteria();
        $c->add(rulesPeer::ID, $request->getParameter('id'));
        $this->rule = rulesPeer::doSelectOne($c);
        $this->ruleData = unserialize($this->rule->getData());
        //$this->pager = new PropelPager($c, 'rulesPeer', 'doSelect', $this->Page, 12);
    }

    public function executeCheckRuleName(sfWebRequest $request) {
        $c = new Criteria();
        $c->add(rulesPeer::NAME, $request->getParameter('name'));
        return $this->renderText(rulesPeer::doSelectOne($c) ? 'ble' : 'oki');
    }

    public function executeAddDbRule(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());

        if ($request->getParameter('type') == 0) {

            $vertic = explode('%', $request->getParameter('vert'));
            $horc = explode('%', $request->getParameter('hor'));

            array_shift($vertic);
            array_shift($horc);

            $cell = explode('%', $request->getParameter('cellv'));
            array_shift($cell);

            $values = array();
            $size = count($cell);

            for ($i = 0; $i < $size; $i+=2) {
                $values[] = array($cell[$i], $cell[$i + 1]);
            }

            $mt = new gameRulesMatrix(
                            $request->getParameter('name'),
                            $request->getParameter('label1'),
                            $request->getParameter('label2'),
                            $vertic,
                            $horc,
                            $values,
                            $request->getParameter('ends'),
                            $request->getParameter('endv'),
                            $request->getParameter('wintype')
            );

            $rul = new Rules();
            $rul->setName($request->getParameter('name'));
            $rul->setData(serialize($mt));
            $rul->setFreepts($request->getParameter('freepts'));
            $rul->setType($request->getParameter('type'));
            $rul->save();
            $this->getUser()->setFlash('notice', 'Zasady zostaly dodane');

            return $this->renderText(var_dump($values));
        } else {

            $content = array();
            $nodes = array();
            $nodes = $request->getParameter('nodes');
            $node = array();
            $label1 = $request->getParameter('label1');
            $label2 = $request->getParameter('label2');

            for ($i = 0; $i < count($nodes); $i++) {
                $node = explode('%', $nodes[$i]);
                $type = array_shift($node);
                $id = (int) array_shift($node);
                switch ($type) {
                    case "0":
                    case "1":
                        $group = (int) array_shift($node);
                        $actions1 = array();
                        while (count($node) > 0) {
                            $a = array_shift($node);
                            $v = (int) array_shift($node);
                            array_push($actions1, array($a, $v));
                        }
                        array_push($content, new treeNodePlayer($id, (int) $type, $group, $type == "0" ? $label1 : $label2, $actions1));
                        break;
                    case "2":
                        $actions2 = array();

                        while (count($node) > 0) {
                            $l = array_shift($node);
                            $p = (float) array_shift($node);
                            $v = (int) array_shift($node);
                            array_push($actions2, array($l, $p, $v));
                        }
                        array_push($content, new treeNodeNature($id, $actions2));
                        break;
                    case "3":
                        array_push($content, new treeNodePoints($id, array((float) $node[0], (float) $node[1])));
                        break;
                }
            }



            $tr = new gameRulesTree($request->getParameter('name'), $label1, $label2, $content, $request->getParameter('ends'), $request->getParameter('endv'), $request->getParameter('wintype'));

            $rul = new Rules();
            $rul->setName($request->getParameter('name'));
            $rul->setData(serialize($tr));
            $rul->setType($request->getParameter('type'));
            $rul->setFreepts($request->getParameter('freepts'));
            $rul->save();
            $this->getUser()->setFlash('notice', 'Zasady zostaly dodane');

            //return $this->renderText(var_dump($content));
        }
    }

    public function executeEditRule(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        $c = new Criteria();
        $c->add(rulesPeer::ID, $request->getParameter('id'));
        $this->rules = rulesPeer::doSelectOne($c);

        if ($this->rules->getType() == 0) {

            $this->matrix = unserialize($this->rules->getData());

            //$this->message = 'TODO: ';

            if ($request->isMethod('post') && $request->hasParameter('rules')) {
                $param = $request->getParameter('rules');
                $this->rules->setName($param['name']);
                $this->rules->setFreepts($param['freepts']);
                /*
                  foreach ($param as $key => $value) {
                  $this->message.=$key . ' ';
                  }
                 */
                $size = $this->matrix->getSize();
                $rows = array();

                for ($i = 0; $i < $size[0]; $i++)
                    $rows[] = $param['rows_' . $i];

                $cols = array();

                for ($i = 0; $i < $size[1]; $i++)
                    $cols[] = $param['cols_' . $i];

                $cells = array();

                $size[1]*=$size[0];

                for ($i = 0; $i < $size[1]; $i++)
                    $cells[] = array($param['cell_' . $i . '_0'], $param['cell_' . $i . '_1']);

                //$this->message.=$param['rows[0]'];

                $this->matrix = new gameRulesMatrix($param['name'], $param['player_a'], $param['player_b'], $rows, $cols, $cells, $param['end_type'], $param['end_value'], $param['win_type']);

                $this->rules->setData(serialize($this->matrix));
                $this->rules->save();
                $this->getUser()->setFlash('notice', 'Zasady zostaly zaktualizowane');
                //$this->redirect('@mod_rules');
            }
        } else {

            //$this->message = 'TODO: ';
            $this->tree = unserialize($this->rules->getData());

            if ($request->isMethod('post') && $request->hasParameter('rules')) {

                $param = $request->getParameter('rules');
                $this->rules->setName($param['name']);
                $this->rules->setFreepts($param['freepts']);

                /*
                  foreach ($param as $key => $value) {
                  $this->message.=$key . $value . ' ';
                  }
                 */
                $this->rep = $this->tree->getContent();

                foreach ($this->rep as $node) {
                    if ($node->getType() == gameRules::$TN_PLAYER1) {
                        $node->setLabel($param['player_a']);
                    }
                    if ($node->getType() == gameRules::$TN_PLAYER2) {
                        $node->setLabel($param['player_b']);
                    }
                }


                $this->tree = new gameRulesTree($param['name'], $param['player_a'], $param['player_b'], $this->rep, $param['end'], $param['end_value'], $param['win']);

                $this->rules->setData(serialize($this->tree));
                $this->rules->save();

                $this->getUser()->setFlash('notice', 'Zasady zostaly zaktualizowane');
            }
        }
        //$this->form=new RulesForm($this->rules);
        //$this->pager = new PropelPager($c, 'rulesPeer', 'doSelect', $this->Page, 8);
    }

    public function executeEditTree(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        $c = new Criteria();
        $c->add(rulesPeer::ID, $request->getParameter('id'));
        $this->rules = rulesPeer::doSelectOne($c);
        $this->tree = unserialize($this->rules->getData());
    }
    
    public function executeCloneRule(sfWebRequest $request) {
      $this->forward404Unless($this->getUser()->isPremium());
      $c = new Criteria();
      $c->add(rulesPeer::ID, $request->getParameter('id'));
      $rule=rulesPeer::doSelectOne($c);
      
      $nrule=new Rules();
      $nrule->setData($rule->getData());
      $nrule->setFreepts($rule->getFreepts());
      $nrule->setType($rule->getType());
      $nrule->setName(substr(sha1(time()),0,10));
      $nrule->save();
      
      //return $this->renderText('');
    }
    
    public function executeDeleteRule(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());
        $c = new Criteria();
        $c->add(rulesPeer::ID, $request->getParameter('id'));
        rulesPeer::doDelete($c);
        //return sfView::NONE;
    }

    public function executeEditDbTree(sfWebRequest $request) {
        $this->forward404Unless($this->getUser()->isPremium());

        $content = array();
        $nodes = array();
        $nodes = $request->getParameter('nodes');
        $node = array();
        $label1 = $request->getParameter('label1');
        $label2 = $request->getParameter('label2');

        for ($i = 0; $i < count($nodes); $i++) {
            $node = explode('%', $nodes[$i]);
            $type = array_shift($node);
            $id = (int) array_shift($node);
            switch ($type) {
                case "0":
                case "1":
                    $group = (int) array_shift($node);
                    $actions1 = array();
                    while (count($node) > 0) {
                        $a = array_shift($node);
                        $v = (int) array_shift($node);
                        array_push($actions1, array($a, $v));
                    }
                    array_push($content, new treeNodePlayer($id, (int) $type, $group, $type == "0" ? $label1 : $label2, $actions1));
                    break;
                case "2":
                    $actions2 = array();

                    while (count($node) > 0) {
                        $l = array_shift($node);
                        $p = (float) array_shift($node);
                        $v = (int) array_shift($node);
                        array_push($actions2, array($l, $p, $v));
                    }
                    array_push($content, new treeNodeNature($id, $actions2));
                    break;
                case "3":
                    array_push($content, new treeNodePoints($id, array((float) $node[0], (float) $node[1])));
                    break;
            }
        }

        $tr = new gameRulesTree($request->getParameter('name'), $label1, $label2, $content, $request->getParameter('ends'), $request->getParameter('endv'), $request->getParameter('wintype'));

        $c = new Criteria();
        $c->add(rulesPeer::ID, $request->getParameter('idr'));
        $this->rules = rulesPeer::doSelectOne($c);

        
        
        $c = new Criteria();
        $c->add(RulesPeer::ID, $request->getParameter('idr'), Criteria::IN);
        $c->add(RulesPeer::DATA, serialize($tr));
        RulesPeer::doUpdate($c);

        //$this->rules->setData(serialize($tr));
        //$this->rules->save();

        //$this->getUser()->setFlash('notice', 'Zasady zostaly zaktualizowane');

        return $this->renderText('Zasady zostaly zaktualizowane');
    }

}
